package com.ruoyi.trainingManagement.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.trainingManagement.domain.Vo.StudentTrainingProgramCoursesVo;
import com.ruoyi.trainingManagement.domain.trainingProgram;
import com.ruoyi.trainingManagement.mapper.trainingProgramMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.trainingManagement.mapper.trainingProgramCoursesMapper;
import com.ruoyi.trainingManagement.domain.trainingProgramCourses;
import com.ruoyi.trainingManagement.service.ItrainingProgramCoursesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 培养计划课程明细Service业务层处理
 * 
 * @author chenxl
 * @date 2025-04-16
 */
@Service
public class trainingProgramCoursesServiceImpl implements ItrainingProgramCoursesService 
{
    @Autowired
    private trainingProgramCoursesMapper trainingProgramCoursesMapper;
    @Autowired
    private trainingProgramMapper trainingProgramMapper;

    /**
     * 查询培养计划课程明细
     * 
     * @param id 培养计划课程明细主键
     * @return 培养计划课程明细
     */
    @Override
    public trainingProgramCourses selecttrainingProgramCoursesById(String id)
    {
        return trainingProgramCoursesMapper.selecttrainingProgramCoursesById(id);
    }

    /**
     * 查询培养计划课程明细列表
     * 
     * @param trainingProgramCourses 培养计划课程明细
     * @return 培养计划课程明细
     */
    @Override
    public List<trainingProgramCourses> selecttrainingProgramCoursesList(trainingProgramCourses trainingProgramCourses)
    {
        return trainingProgramCoursesMapper.selecttrainingProgramCoursesList(trainingProgramCourses);
    }

    /**
     * 新增培养计划课程明细
     * 
     * @param trainingProgramCourses 培养计划课程明细
     * @return 结果
     */
    @Override
    public int inserttrainingProgramCourses(trainingProgramCourses trainingProgramCourses)
    {
        return trainingProgramCoursesMapper.inserttrainingProgramCourses(trainingProgramCourses);
    }

    /**
     * 修改培养计划课程明细
     * 
     * @param trainingProgramCourses 培养计划课程明细
     * @return 结果
     */
    @Override
    public int updatetrainingProgramCourses(trainingProgramCourses trainingProgramCourses)
    {
        return trainingProgramCoursesMapper.updatetrainingProgramCourses(trainingProgramCourses);
    }

    /**
     * 批量删除培养计划课程明细
     * 
     * @param ids 需要删除的培养计划课程明细主键
     * @return 结果
     */
    @Override
    public int deletetrainingProgramCoursesByIds(String[] ids)
    {
        return trainingProgramCoursesMapper.deletetrainingProgramCoursesByIds(ids);
    }

    /**
     * 删除培养计划课程明细信息
     * 
     * @param id 培养计划课程明细主键
     * @return 结果
     */
    @Override
    public int deletetrainingProgramCoursesById(String id)
    {
        return trainingProgramCoursesMapper.deletetrainingProgramCoursesById(id);
    }

    @Override
    public List<trainingProgramCourses> selecttrainingProgramCoursesByTrainingProgramId(String trainingProgramId)
    {
        //return trainingProgramCoursesMapper.selecttrainingProgramCoursesByTrainingProgramId(trainingProgramId);
        System.out.println("Querying courses for trainingProgramId: " + trainingProgramId); // 添加日志
        List<trainingProgramCourses> list = trainingProgramCoursesMapper.selecttrainingProgramCoursesByTrainingProgramId(trainingProgramId);
        System.out.println("Fetched courses: " + list); // 添加日志
        return list;
    }
    /**
     * 批量增加培养计划课程明细
     * @param trainingProgramCoursesList 课程明细列表
     * @return 影响的行数
     */
    @Override
    public int batchInsertTrainingProgramCourses(List<trainingProgramCourses> trainingProgramCoursesList) {
        return trainingProgramCoursesMapper.batchInsertTrainingProgramCourses(trainingProgramCoursesList);
    }

    /**
     * 批量更新培养计划课程的状态、班级数量和最大人数
     * @param trainingProgramList 培养计划列表
     * @param courseId 课程ID
     * @param numberClasses 班级数量
     * @param numberStudents 班级最大人数
     * @return 成功更新的记录数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByProgramAndCourseId(List<trainingProgram> trainingProgramList,
                                          String courseId,
                                          Integer numberClasses,
                                          Integer numberStudents) {
        // 1. 参数校验
        if (trainingProgramList == null || trainingProgramList.isEmpty()) {
            throw new IllegalArgumentException("培养计划列表不能为空");
        }
        if (courseId == null || courseId.trim().isEmpty()) {
            throw new IllegalArgumentException("课程ID不能为空");
        }
        if (numberClasses == null || numberClasses <= 0) {
            throw new IllegalArgumentException("班级数量必须为正整数");
        }
        if (numberStudents == null || numberStudents <= 0) {
            throw new IllegalArgumentException("班级最大人数必须为正整数");
        }

        // 2. 定义状态值（已编排）
        int status = 1;
        int successCount = 0;

        // 3. 批量更新并统计成功次数
        for (trainingProgram trainingProgram : trainingProgramList) {
            String trainingProgramId = trainingProgram.getId();
            // 校验培养计划ID
            if (trainingProgramId == null || trainingProgramId.trim().isEmpty()) {
                continue; // 跳过ID无效的记录
            }

            // 执行更新并累加成功次数
            int rows = trainingProgramCoursesMapper.updateByProgramAndCourseId(
                    trainingProgramId,
                    courseId,
                    status,
                    numberClasses,
                    numberStudents
            );
            successCount += rows;
        }

        // 4. 校验更新结果
        if (successCount == 0) {
            throw new RuntimeException("未找到匹配的培养计划课程记录，更新失败");
        }

        return successCount;
    }

    /**
     * 根据培养方案ID和课程ID查询对应的培养方案课程ID
     * @param trainingProgramIds 培养方案ID
     * @param courseId 课程ID
     * @return 培养方案课程ID（training_program_courses表的id）
     */
    @Override
    public List<String> selectIdsByProgramIdsAndCourseId(List<String> trainingProgramIds, String courseId) {
        return trainingProgramCoursesMapper.selectIdsByProgramIdsAndCourseId(trainingProgramIds, courseId);
    }

    /**
     * 根据培养方案ID查询StudentTrainingProgramCoursesVo
     * @param trainingProgramId 培养方案ID
     *
     * @return StudentTrainingProgramCoursesVo
     */
    @Override
    public List<StudentTrainingProgramCoursesVo> selectStudentTrainingProgramCoursesVoByTrainingProgramId(String trainingProgramId){return trainingProgramCoursesMapper.selectStudentTrainingProgramCoursesVoByTrainingProgramId(trainingProgramId);}

    @Override
    public boolean checkAllProgramsCourseApproved() {
        // 查询是否存在未审核通过的记录
        int count = trainingProgramCoursesMapper.countUnapprovedProgramCourse();
        System.out.println("trainingProgramCoursesMapper.countUnapprovedProgramCourse查询到的count: " + count);
        // 如果不存在未审核通过的记录，返回true
        return count == 0;
    }

    /**
     * 根据ID修改状态
     * @param id 课程排课详情ID
     * @param status 要更新的状态值
     * @return 影响行数
     */
    @Override
    public int updateStatusById(String id, int status) {
        return trainingProgramCoursesMapper.updateStatusById(id, status);
    }
    /**
     * 根据状态列表查询课程列表
     * @param statusList 状态列表（不能为空或空列表）
     * @return 课程列表
     */
    @Override
    public List<trainingProgramCourses> selectByStatusList(List<Integer> statusList) {
        // 参数校验
        if (statusList == null || statusList.isEmpty()) {
            //throw new BusinessException("状态列表不能为空");
            return new ArrayList<>();
        }
        // 调用批量查询方法（一次SQL查询，替代循环查询）
        return trainingProgramCoursesMapper.selectByStatusList(statusList);
    }
    //根据学院列表获取对应的全部培养方案课程列表
    @Override
    public List<trainingProgramCourses> selectCoursesByCollegeIds(List<String> collegeIds) {
        List<String> trainingProgramIds=trainingProgramMapper.selectOnlyIdsByCollegeIds(collegeIds);
        List<trainingProgramCourses>  courses=trainingProgramCoursesMapper.selectCoursesByProgramIds(trainingProgramIds);
        return courses;
    }
    //根据学院代码列表获取对应全部培养方案课程列表
    @Override
    public List<trainingProgramCourses> selectCoursesByCollegeCodes(List<String> collegeCodes) {
       List<trainingProgramCourses> courses=new ArrayList<>();
        return courses;
    }
    //根据学院ID列表联查培养方案课程列表
    @Override
    public List<trainingProgramCourses> selectTrainingProgramCoursesByCollegeIds(List<String> collegeIds) {
        return trainingProgramCoursesMapper.selectTrainingProgramCoursesByCollegeIds(collegeIds);
    }
}
